from utils import *
import time

XUAN_ZE_DO = 1
ZHAN_DOU=2
SHI_YONG=3
GOU_MAI=4
        

class Base(Device):
    EventHuodong=Event('./pic/base/huodong.png',RECTS.TopHalf)
    EventZhiyin=Event('./pic/base/zhiyin.png',RECTS.TopHalf)
    EventShiyong=Event('./pic/base/shiyong.png',RECTS.RightHalf)
    EventGuajiQuxiao=Event('./pic/base/guaji_quxiao.png',RECTS.BottomHalf)
    
    EventBaitan=Event('./pic/base/baitan.png',RECTS.TopHalf)
    EventZidong=Event('./pic/base/zidong.png',RECTS.BottomHalf)
    EventGoumai=Event('./pic/base/goumai.png',RECTS.BottomHalf)
    EventGoumai2=Event('./pic/base/goumai2.png',RECTS.BottomHalf)
    EventGoumaiXuqiu=Event('./pic/base/goumai_xuqiu.png',RECTS.TopHalf)
    EventShangjiao=Event('./pic/base/shangjiao.png',RECTS.BottomHalf)
    EventJixiBtn=Event('./pic/base/jixu_btn.png',RECTS.TopHalf)
    EventRenwushuxing=Event('./pic/base/renwushuxing.png',RECTS.TopHalf)
    EventJiarubangpai=Event('./pic/base/jiarubangpai.png',RECTS.TopHalf)
    EventBeginCancel=[
        Event('./pic/base/begin_guanbi1.png',RECTS.RightHalf),
        Event('./pic/base/begin_guanbi2.png',RECTS.RightHalf),
    ]
    # def waitRun(self):
    #     while True:
    #         if self.isHomePage():
    #             self.flush()
    #             img1=self.image()[60:80,149:202]
    #             time.sleep(0.5)
    #             self.flush()
    #             img2=self.image()[60:80,149:202]
    #             if self.isCmpare(img1,img2):
    #                 break
    #             else:
    #                 print('跑图中...')
    #         else:
    #             break

    def waitFight(self):
        print('战斗中...')
        if self.find(self.EventZidong):
            self.click(*self.EventZidong.Position())
            time.sleep(0.2)
        while self.find(self.EventGuajiQuxiao):
            time.sleep(2)
            self.screenShot()
            self.UseImg()
        print('战斗结束')

    def isHomePage(self): 
        # print('查找',self.EventZhiyin.file)
        # lx, ly, rx, ry=self.EventZhiyin.Rect
        # cv2.imshow('image.png',self.image())

        # cv2.imshow('rect.png',self.image()[ly: ry, lx: rx])
        # cv2.imshow('zhiyin.png',self.EventZhiyin.Img)
        # cv2.waitKey(0)
        # if self.find(self.EventZhiyin,0.6):
        #     print('找到',self.EventZhiyin.file)
        #     print(self.EventZhiyin.Position())
        if self.find(self.EventHuodong) or self.find(self.EventZhiyin):
            return True
        return False
    def waitStop(self):
        tmpImg=self.img
        time.sleep(1)
        while not self.isCmpare(self.img[60:80,149:202],tmpImg[60:80,149:202]):
            tmpImg=self.img
            time.sleep(1)

    def waitFlush(self):
        tmpImg=self.img
        while self.isCmpare(self.useImg[0:85,0:230],tmpImg[0:85,0:230])\
                and self.isCmpare(self.useImg[600:720,530:1280],tmpImg[600:720,530:1280]):
            tmpImg=self.img
            time.sleep(0.2)
    
    def BaseRun(self):
        #使用
        if self.find(self.EventShiyong):
            print('使用物品')
            self.click(*self.EventShiyong.Position())
            time.sleep(2)
            return True
        elif self.find(self.EventGoumai):
        #购买1
            print('购买物品')
            # if self.find(self.EventGoumaiXuqiu):
            #     self.click(*self.EventGoumaiXuqiu.Position())
            self.click(*self.EventGoumai.Position())
            time.sleep(0.5)
            return True
        elif  self.find(self.EventGoumai2):
        #购买2
            print('购买物品')
            # if self.find(self.EventGoumaiXuqiu):
            #         self.click(*self.EventGoumaiXuqiu.Position())
            self.click(*self.EventGoumai2.Position())
            time.sleep(0.5)
            return True
        elif self.find(self.EventShangjiao):
        #上交
            print('上交物品')
            self.click(*self.EventShangjiao.Position())
            time.sleep(1)
            self.click(*self.EventShangjiao.Position())
            return True
        #TODO 跳过剧情
        elif self.find(self.EventJixiBtn):
            print('跳过剧情')
            self.click(*self.EventJixiBtn.Position())
            time.sleep(0.2)
            return True
        elif self.find(self.EventRenwushuxing):
            print('关闭人物属性窗口')
            self.click(1093,47)
            time.sleep(1)
            return True
        elif self.find(self.EventJiarubangpai):
            print('关闭加入帮派窗口')
            self.click(1093,47)
            time.sleep(1)
            
        return False
        
    def Begin(self):
        print('等待进入主界面...')
        while not self.isHomePage():
            for event in self.EventBeginCancel:
                if self.find(event):
                    self.click(*event.Position())
                    time.sleep(0.2)
                    self.flush()
                    self.UseImg()
        print('进入主界面')
        